package pe.gob.mpfn.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.component.html.HtmlPanelGrid;
import javax.faces.context.FacesContext;
import org.primefaces.component.autocomplete.AutoComplete;
import org.primefaces.component.message.Message;
import org.primefaces.context.RequestContext;
import org.primefaces.event.CloseEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.jsf.managedbean.RooJsfManagedBean;
import org.springframework.roo.addon.serializable.RooSerializable;
import pe.gob.mpfn.controller.converter.ExpedienteConverter;
import pe.gob.mpfn.controller.converter.ParteConverter;
import pe.gob.mpfn.model.Expediente;
import pe.gob.mpfn.model.ExpedienteParte;
import pe.gob.mpfn.model.Parte;
import pe.gob.mpfn.service.ExpedienteParteService;
import pe.gob.mpfn.service.ExpedienteService;
import pe.gob.mpfn.service.ParteService;

@Configurable
@ManagedBean(name = "expedienteParteBean")
@SessionScoped
@RooSerializable
@RooJsfManagedBean(entity = ExpedienteParte.class, beanName = "expedienteParteBean")
public class ExpedienteParteBean implements Serializable  {

	private static final long serialVersionUID = 1L;

	@Autowired
    ExpedienteParteService expedienteParteService;

	@Autowired
    ExpedienteService expedienteService;

	@Autowired
    ParteService parteService;

	private String name = "ExpedientePartes";

	private ExpedienteParte expedienteParte;

	private List<ExpedienteParte> allExpedientePartes;

	private boolean dataVisible = false;

	private List<String> columns;

	private HtmlPanelGrid createPanelGrid;

	private HtmlPanelGrid editPanelGrid;

	private HtmlPanelGrid viewPanelGrid;

	private boolean createDialogVisible = false;

	@PostConstruct
    public void init() {
        columns = new ArrayList<String>();
    }

	public String getName() {
        return name;
    }

	public List<String> getColumns() {
        return columns;
    }

	public List<ExpedienteParte> getAllExpedientePartes() {
        return allExpedientePartes;
    }

	public void setAllExpedientePartes(List<ExpedienteParte> allExpedientePartes) {
        this.allExpedientePartes = allExpedientePartes;
    }

	public String findAllExpedientePartes() {
        allExpedientePartes = expedienteParteService.findAllExpedientePartes();
        dataVisible = !allExpedientePartes.isEmpty();
        return null;
    }

	public boolean isDataVisible() {
        return dataVisible;
    }

	public void setDataVisible(boolean dataVisible) {
        this.dataVisible = dataVisible;
    }

	public HtmlPanelGrid getCreatePanelGrid() {
        if (createPanelGrid == null) {
            createPanelGrid = populateCreatePanel();
        }
        return createPanelGrid;
    }

	public void setCreatePanelGrid(HtmlPanelGrid createPanelGrid) {
        this.createPanelGrid = createPanelGrid;
    }

	public HtmlPanelGrid getEditPanelGrid() {
        if (editPanelGrid == null) {
            editPanelGrid = populateEditPanel();
        }
        return editPanelGrid;
    }

	public void setEditPanelGrid(HtmlPanelGrid editPanelGrid) {
        this.editPanelGrid = editPanelGrid;
    }

	public HtmlPanelGrid getViewPanelGrid() {
        if (viewPanelGrid == null) {
            viewPanelGrid = populateViewPanel();
        }
        return viewPanelGrid;
    }

	public void setViewPanelGrid(HtmlPanelGrid viewPanelGrid) {
        this.viewPanelGrid = viewPanelGrid;
    }

	public HtmlPanelGrid populateCreatePanel() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Application application = facesContext.getApplication();
        ExpressionFactory expressionFactory = application.getExpressionFactory();
        ELContext elContext = facesContext.getELContext();
        
        HtmlPanelGrid htmlPanelGrid = (HtmlPanelGrid) application.createComponent(HtmlPanelGrid.COMPONENT_TYPE);
        
        HtmlOutputText strNumeroExpedienteIdCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strNumeroExpedienteIdCreateOutput.setId("strNumeroExpedienteIdCreateOutput");
        strNumeroExpedienteIdCreateOutput.setValue("Str Numero Expediente Id:   ");
        htmlPanelGrid.getChildren().add(strNumeroExpedienteIdCreateOutput);
        
        AutoComplete strNumeroExpedienteIdCreateInput = (AutoComplete) application.createComponent(AutoComplete.COMPONENT_TYPE);
        strNumeroExpedienteIdCreateInput.setId("strNumeroExpedienteIdCreateInput");
        strNumeroExpedienteIdCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{expedienteParteBean.expedienteParte.strNumeroExpedienteId}", Expediente.class));
        strNumeroExpedienteIdCreateInput.setCompleteMethod(expressionFactory.createMethodExpression(elContext, "#{expedienteParteBean.completeStrNumeroExpedienteId}", List.class, new Class[] { String.class }));
        strNumeroExpedienteIdCreateInput.setDropdown(true);
        strNumeroExpedienteIdCreateInput.setValueExpression("var", expressionFactory.createValueExpression(elContext, "strNumeroExpedienteId", String.class));
        strNumeroExpedienteIdCreateInput.setValueExpression("itemLabel", expressionFactory.createValueExpression(elContext, "#{strNumeroExpedienteId.strLugarSuceso} #{strNumeroExpedienteId.datFechaRegistro} #{strNumeroExpedienteId.datFechaSuceso} #{strNumeroExpedienteId.strNumeroExpedienteId}", String.class));
        strNumeroExpedienteIdCreateInput.setValueExpression("itemValue", expressionFactory.createValueExpression(elContext, "#{strNumeroExpedienteId}", Expediente.class));
        strNumeroExpedienteIdCreateInput.setConverter(new ExpedienteConverter());
        strNumeroExpedienteIdCreateInput.setRequired(false);
        htmlPanelGrid.getChildren().add(strNumeroExpedienteIdCreateInput);
        
        Message strNumeroExpedienteIdCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        strNumeroExpedienteIdCreateInputMessage.setId("strNumeroExpedienteIdCreateInputMessage");
        strNumeroExpedienteIdCreateInputMessage.setFor("strNumeroExpedienteIdCreateInput");
        strNumeroExpedienteIdCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(strNumeroExpedienteIdCreateInputMessage);
        
        HtmlOutputText intParteIdCreateOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        intParteIdCreateOutput.setId("intParteIdCreateOutput");
        intParteIdCreateOutput.setValue("Int Parte Id:   ");
        htmlPanelGrid.getChildren().add(intParteIdCreateOutput);
        
        AutoComplete intParteIdCreateInput = (AutoComplete) application.createComponent(AutoComplete.COMPONENT_TYPE);
        intParteIdCreateInput.setId("intParteIdCreateInput");
        intParteIdCreateInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{expedienteParteBean.expedienteParte.intParteId}", Parte.class));
        intParteIdCreateInput.setCompleteMethod(expressionFactory.createMethodExpression(elContext, "#{expedienteParteBean.completeIntParteId}", List.class, new Class[] { String.class }));
        intParteIdCreateInput.setDropdown(true);
        intParteIdCreateInput.setValueExpression("var", expressionFactory.createValueExpression(elContext, "intParteId", String.class));
        intParteIdCreateInput.setValueExpression("itemLabel", expressionFactory.createValueExpression(elContext, "#{intParteId.strNombre} #{intParteId.strApellidoParterno} #{intParteId.strApellidoMaterno} #{intParteId.intEdad}", String.class));
        intParteIdCreateInput.setValueExpression("itemValue", expressionFactory.createValueExpression(elContext, "#{intParteId}", Parte.class));
        intParteIdCreateInput.setConverter(new ParteConverter());
        intParteIdCreateInput.setRequired(false);
        htmlPanelGrid.getChildren().add(intParteIdCreateInput);
        
        Message intParteIdCreateInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        intParteIdCreateInputMessage.setId("intParteIdCreateInputMessage");
        intParteIdCreateInputMessage.setFor("intParteIdCreateInput");
        intParteIdCreateInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(intParteIdCreateInputMessage);
        
        return htmlPanelGrid;
    }

	public HtmlPanelGrid populateEditPanel() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Application application = facesContext.getApplication();
        ExpressionFactory expressionFactory = application.getExpressionFactory();
        ELContext elContext = facesContext.getELContext();
        
        HtmlPanelGrid htmlPanelGrid = (HtmlPanelGrid) application.createComponent(HtmlPanelGrid.COMPONENT_TYPE);
        
        HtmlOutputText strNumeroExpedienteIdEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strNumeroExpedienteIdEditOutput.setId("strNumeroExpedienteIdEditOutput");
        strNumeroExpedienteIdEditOutput.setValue("Str Numero Expediente Id:   ");
        htmlPanelGrid.getChildren().add(strNumeroExpedienteIdEditOutput);
        
        AutoComplete strNumeroExpedienteIdEditInput = (AutoComplete) application.createComponent(AutoComplete.COMPONENT_TYPE);
        strNumeroExpedienteIdEditInput.setId("strNumeroExpedienteIdEditInput");
        strNumeroExpedienteIdEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{expedienteParteBean.expedienteParte.strNumeroExpedienteId}", Expediente.class));
        strNumeroExpedienteIdEditInput.setCompleteMethod(expressionFactory.createMethodExpression(elContext, "#{expedienteParteBean.completeStrNumeroExpedienteId}", List.class, new Class[] { String.class }));
        strNumeroExpedienteIdEditInput.setDropdown(true);
        strNumeroExpedienteIdEditInput.setValueExpression("var", expressionFactory.createValueExpression(elContext, "strNumeroExpedienteId", String.class));
        strNumeroExpedienteIdEditInput.setValueExpression("itemLabel", expressionFactory.createValueExpression(elContext, "#{strNumeroExpedienteId.strLugarSuceso} #{strNumeroExpedienteId.datFechaRegistro} #{strNumeroExpedienteId.datFechaSuceso} #{strNumeroExpedienteId.strNumeroExpedienteId}", String.class));
        strNumeroExpedienteIdEditInput.setValueExpression("itemValue", expressionFactory.createValueExpression(elContext, "#{strNumeroExpedienteId}", Expediente.class));
        strNumeroExpedienteIdEditInput.setConverter(new ExpedienteConverter());
        strNumeroExpedienteIdEditInput.setRequired(false);
        htmlPanelGrid.getChildren().add(strNumeroExpedienteIdEditInput);
        
        Message strNumeroExpedienteIdEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        strNumeroExpedienteIdEditInputMessage.setId("strNumeroExpedienteIdEditInputMessage");
        strNumeroExpedienteIdEditInputMessage.setFor("strNumeroExpedienteIdEditInput");
        strNumeroExpedienteIdEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(strNumeroExpedienteIdEditInputMessage);
        
        HtmlOutputText intParteIdEditOutput = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        intParteIdEditOutput.setId("intParteIdEditOutput");
        intParteIdEditOutput.setValue("Int Parte Id:   ");
        htmlPanelGrid.getChildren().add(intParteIdEditOutput);
        
        AutoComplete intParteIdEditInput = (AutoComplete) application.createComponent(AutoComplete.COMPONENT_TYPE);
        intParteIdEditInput.setId("intParteIdEditInput");
        intParteIdEditInput.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{expedienteParteBean.expedienteParte.intParteId}", Parte.class));
        intParteIdEditInput.setCompleteMethod(expressionFactory.createMethodExpression(elContext, "#{expedienteParteBean.completeIntParteId}", List.class, new Class[] { String.class }));
        intParteIdEditInput.setDropdown(true);
        intParteIdEditInput.setValueExpression("var", expressionFactory.createValueExpression(elContext, "intParteId", String.class));
        intParteIdEditInput.setValueExpression("itemLabel", expressionFactory.createValueExpression(elContext, "#{intParteId.strNombre} #{intParteId.strApellidoParterno} #{intParteId.strApellidoMaterno} #{intParteId.intEdad}", String.class));
        intParteIdEditInput.setValueExpression("itemValue", expressionFactory.createValueExpression(elContext, "#{intParteId}", Parte.class));
        intParteIdEditInput.setConverter(new ParteConverter());
        intParteIdEditInput.setRequired(false);
        htmlPanelGrid.getChildren().add(intParteIdEditInput);
        
        Message intParteIdEditInputMessage = (Message) application.createComponent(Message.COMPONENT_TYPE);
        intParteIdEditInputMessage.setId("intParteIdEditInputMessage");
        intParteIdEditInputMessage.setFor("intParteIdEditInput");
        intParteIdEditInputMessage.setDisplay("icon");
        htmlPanelGrid.getChildren().add(intParteIdEditInputMessage);
        
        return htmlPanelGrid;
    }

	public HtmlPanelGrid populateViewPanel() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Application application = facesContext.getApplication();
        ExpressionFactory expressionFactory = application.getExpressionFactory();
        ELContext elContext = facesContext.getELContext();
        
        HtmlPanelGrid htmlPanelGrid = (HtmlPanelGrid) application.createComponent(HtmlPanelGrid.COMPONENT_TYPE);
        
        HtmlOutputText strNumeroExpedienteIdLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strNumeroExpedienteIdLabel.setId("strNumeroExpedienteIdLabel");
        strNumeroExpedienteIdLabel.setValue("Str Numero Expediente Id:   ");
        htmlPanelGrid.getChildren().add(strNumeroExpedienteIdLabel);
        
        HtmlOutputText strNumeroExpedienteIdValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        strNumeroExpedienteIdValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{expedienteParteBean.expedienteParte.strNumeroExpedienteId}", Expediente.class));
        strNumeroExpedienteIdValue.setConverter(new ExpedienteConverter());
        htmlPanelGrid.getChildren().add(strNumeroExpedienteIdValue);
        
        HtmlOutputText intParteIdLabel = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        intParteIdLabel.setId("intParteIdLabel");
        intParteIdLabel.setValue("Int Parte Id:   ");
        htmlPanelGrid.getChildren().add(intParteIdLabel);
        
        HtmlOutputText intParteIdValue = (HtmlOutputText) application.createComponent(HtmlOutputText.COMPONENT_TYPE);
        intParteIdValue.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{expedienteParteBean.expedienteParte.intParteId}", Parte.class));
        intParteIdValue.setConverter(new ParteConverter());
        htmlPanelGrid.getChildren().add(intParteIdValue);
        
        return htmlPanelGrid;
    }

	public ExpedienteParte getExpedienteParte() {
        if (expedienteParte == null) {
            expedienteParte = new ExpedienteParte();
        }
        return expedienteParte;
    }

	public void setExpedienteParte(ExpedienteParte expedienteParte) {
        this.expedienteParte = expedienteParte;
    }

	public List<Expediente> completeStrNumeroExpedienteId(String query) {
        List<Expediente> suggestions = new ArrayList<Expediente>();
        for (Expediente expediente : expedienteService.findAllExpedientes()) {
            String expedienteStr = String.valueOf(expediente.getStrLugarSuceso() +  " "  + expediente.getDatFechaRegistro() +  " "  + expediente.getDatFechaSuceso() +  " "  + expediente.getStrNumeroExpedienteId());
            if (expedienteStr.toLowerCase().startsWith(query.toLowerCase())) {
                suggestions.add(expediente);
            }
        }
        return suggestions;
    }

	public List<Parte> completeIntParteId(String query) {
        List<Parte> suggestions = new ArrayList<Parte>();
        for (Parte parte : parteService.findAllPartes()) {
            String parteStr = String.valueOf(parte.getStrNombre() +  " "  + parte.getStrApellidoParterno() +  " "  + parte.getStrApellidoMaterno() +  " "  + parte.getIntEdad());
            if (parteStr.toLowerCase().startsWith(query.toLowerCase())) {
                suggestions.add(parte);
            }
        }
        return suggestions;
    }

	public String onEdit() {
        return null;
    }

	public boolean isCreateDialogVisible() {
        return createDialogVisible;
    }

	public void setCreateDialogVisible(boolean createDialogVisible) {
        this.createDialogVisible = createDialogVisible;
    }

	public String displayList() {
        createDialogVisible = false;
        findAllExpedientePartes();
        return "expedienteParte";
    }

	public String displayCreateDialog() {
        expedienteParte = new ExpedienteParte();
        createDialogVisible = true;
        return "expedienteParte";
    }

	public String persist() {
        String message = "";
        if (expedienteParte.getIntExpedienteParteId() != null) {
            expedienteParteService.updateExpedienteParte(expedienteParte);
            message = "Successfully updated";
        } else {
            expedienteParteService.saveExpedienteParte(expedienteParte);
            message = "Successfully created";
        }
        RequestContext context = RequestContext.getCurrentInstance();
        context.execute("createDialog.hide()");
        context.execute("editDialog.hide()");
        
        FacesMessage facesMessage = new FacesMessage(message);
        FacesContext.getCurrentInstance().addMessage(null, facesMessage);
        reset();
        return findAllExpedientePartes();
    }

	public String delete() {
        expedienteParteService.deleteExpedienteParte(expedienteParte);
        FacesMessage facesMessage = new FacesMessage("Successfully deleted");
        FacesContext.getCurrentInstance().addMessage(null, facesMessage);
        reset();
        return findAllExpedientePartes();
    }

	public void reset() {
        expedienteParte = null;
        createDialogVisible = false;
    }

	public void handleDialogClose(CloseEvent event) {
        reset();
    }
}
